#include "StdAfx.h"
#include "ClearAgenda.h"
#include "DeleteAgenda.h"
#include "UserDoesNotExist.h"
#include "InvalidPassword.h"
#include "UnableToDelete.h"
#include "MeetingDoesNotExist.h"

ClearAgenda::ClearAgenda(const string& name, const string& password)
{
	this->name = name;
	this->password = password;
}


ClearAgenda::~ClearAgenda(void)
{
}

bool ClearAgenda::doAction(UserCollection& uc, MeetingCollection& mc)
{
	//guard
	if( ! uc.hasUser(name) )
		throw UserDoesNotExist();
	if( ! uc.isValid(name, password) )
		throw InvalidPassword();

	User &inviter = uc.getUserByName(name);

	set<int> meetingIds = inviter.getMeetingIds();
	if( meetingIds.empty() )
		throw MeetingDoesNotExist();

	int id;

	set<int>::iterator it = meetingIds.begin();

	while( it != meetingIds.end() )
	{
		id = *it;
		Meeting meeting = mc.getMeetingById(id);

		User &invitee = meeting.getInvitee();

		if( inviter.getId() != invitee.getId() )
		{
			mc.deleteMeetingById(id);			//delete the meeting in the meetingCollection
			inviter.deleteMeetingId(id);		//delete the meeting in the inviter's meeting list
			invitee.deleteMeetingId(id);		//delete the meeting int the invitee's meeting list
		}

		it++;	//get the updated meetingIds
	}

	return true;
}//modify